
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  property;

import java.sql.ResultSet;
import java.util.Hashtable;

import logger.PoolLogger;

import control.SpecialCharacter;

import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;
import utility.*;

public class Order 
{
	GetIdName gid;
	String ORDER="Order";
	String SEQUENCE="sequence";
	//SearchRecord sr;
	RecordSearch ps;
	PoolLogger pl;
	
	public Order() 
	{
		gid = new GetIdName();
		ORDER = gid.getId(ORDER);
		SEQUENCE = gid.getId(SEQUENCE);
		
		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	
	
	public Hashtable<Object, Object> getSequences(String Fieldname)
	{
		Hashtable<Object, Object >table = new Hashtable<Object, Object>();
		//String path = Input.MASTERREPORTSPATH +  Fieldname + "//" + Fieldname +"_mid" + Input.FILRFORMAT;
		table.put("pid", SEQUENCE);
		table.put("mid", Fieldname);
		try 
		{
			//RandomAccessFile raf = new RandomAccessFile(path,"r");
			//FileUtility  fu = new FileUtility(raf);
//			int indexpv = fu.getColumnIndex("pv")-1;
//			int indexvt = fu.getColumnIndex("vt")-1;
//			int indexref = fu.getColumnIndex("ref")-1;
//			
			int indexpv = Column.pv_index-1;
			int indexvt = Column.vt_index-1;
			int indexref = Column.ref_index-1;
			//raf.close();
			
			/*sr = new SearchRecord(path);
			sr.fileOpen();
			String line[]= sr.getRecordSet(table);
			sr.fileClose();*/
			table.put("td","null");
			ps = new RecordSearch();
			ps.setConditionMap(table);
			String line[] = ps.getArrayFromResultSet();
			String Sequence="";
			for(int i=0;i<line.length;i++)
			{
				String Split[]= line[i].split("\t");
				Sequence=Split[indexpv];
				if(Sequence.equals("0"))
					Sequence = Split[indexvt];
			}
			//path =  Input.MASTERREPORTSPATH +  Sequence + "//" + Sequence +"_mid" + Input.FILRFORMAT;
			table.clear();
			table.put("pid", ORDER);
			table.put("mid", Sequence);
			/*sr= new SearchRecord(path);
			sr.fileOpen();
			String fileline[]=sr.getRecordSet(table);
			sr.fileClose();*/
			table.put("td","null");
			ps = new RecordSearch();
			ps.setConditionMap(table);
			String fileline[] = ps.getArrayFromResultSet();
			table.clear();
			table.put(Integer.toString(1), "type");
			for(int i=0;i<fileline.length;i++)
			{
				String Split[] =fileline[i].split("\t");
				String key = gid.getItem(Split[indexpv]);
				String value= gid.getItem(Split[indexref]);
				table.put(value, key);
			}
			
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			pl.info(e.getMessage());
			pl.debug(e.getMessage());
			pl.warn(e.getMessage());
			pl.fatal(e.getMessage());
			pl.error(e.getMessage());
			pl.trace(e.getMessage());
		}
		System.out.println("The Sequences is :"+table);
		pl.info("The Sequences is :"+table);
		pl.debug("The Sequences is :"+table);
		pl.warn("The Sequences is :"+table);
		pl.fatal("The Sequences is :"+table);
		pl.error("The Sequences is :"+table);
		pl.trace("The Sequences is :"+table);
		return table;
	}
	
	
	public String getMasterMessage(String Tablename, String message,String process)
	{
		 Tablename = "property_details";
		Hashtable<Object, Object>table= new Hashtable<Object, Object>();
		StringBuffer Resultmsg = new StringBuffer(message);
		try 
		{
			ResultSet rs  = DBManager.getSelect("select * from command where  msgtype='"+ Tablename +"' and process ='"+process+"' and order1>0 order by order1");
			boolean flag=true;
			while(rs.next())
			{
				String Spchar = rs.getString("spchar");
				if(message.contains(Spchar) & flag==true)
				{	
					  return  message;
				}	  
				else
				{
					SpecialCharacter command = new SpecialCharacter();
					int count=0,index=0;
					int length=0;
					if(flag==false)
					do
					{
						
						if(count==0)
						{
							index= Resultmsg.indexOf(Spchar);
							int ind = Resultmsg.indexOf(Spchar,index+1);
							if(ind == -1)
								ind = command.getNextSpChar(index,Resultmsg.toString().toCharArray());
							String Type= Resultmsg.substring(index+1,ind);
							if(Type.length()!=0)
							{
								Type = gid.getId(Type);
								table = getSequences(Type);
								return message;
							}	
						}	
						else
						{
							index = Resultmsg.indexOf(Spchar,index+length+1);
							@SuppressWarnings("unused")
							char temp =Resultmsg.charAt(index+1);
							while(Character.toString(Resultmsg.charAt(index+1)).equals(Spchar))
							{	
								StringBuffer temp1 =new StringBuffer();
								temp1.append(Resultmsg.substring(index+1));
								Resultmsg = new StringBuffer(Resultmsg.substring(0,index));
								Resultmsg.append(temp1);
								index = Resultmsg.indexOf(Spchar,index);
								count++;
							}
							int i=command.getNextSpChar(index,Resultmsg.toString().toCharArray());
							if(i==(index+1))
							{
								Resultmsg.replace(index,index+1, "");
								count++;
							}	
						}
						rs.previous();
						String prespchar  =  rs.getString("spchar");
						rs.next();
						length = (prespchar+ table.get(Integer.toString(count+1)).toString()).length();
						String beforeafter= rs.getString("beforeafter");
						if(beforeafter.equals("after"))
							Resultmsg.insert(index,prespchar+table.get(Integer.toString(count+1)));
						else if(beforeafter.equals("before"))
							Resultmsg.insert(index,table.get(Integer.toString(count+1))+prespchar);
						System.out.println(Resultmsg);
						count++;
					}
					while(Resultmsg.indexOf(Spchar,index+length+1)!=-1);
					
					flag = false;
				}
			}
		}
		catch (Exception e) 
		{	
		}
		return  Resultmsg.toString();
	}
	
	
	public static void main(String[] args) 
	{
		Order or = new Order();
		//System.out.println(or.getMasterMessage("property_details", "nm*\"Dummy_Lux 1 Unit 20\":item#605"));
		System.out.println(or.getMasterMessage("master_table", "nm*\"Item Sample 20 ml\":item:xpp:::::\"Sample Milk 19\"#605","add"));
	}
	

}
